# coding: utf-8

import base64
import datetime
import hashlib

from common.seven_moor.constant import ACCOUNT_ID, ACCOUNT_API_SECRET


class Token(object):
    """
    create by zhouhl 2018-01-08
    七陌认证类
    """

    def __init__(self):
        self.account_id = ACCOUNT_ID
        self.account_api_secret = ACCOUNT_API_SECRET
        self.timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')

    def create_authorization(self):
        """
        生成七陌请求头中的 Authorization 信息
        """
        base_str = '{account_id}:{timestamp}'.format(
            account_id = self.account_id,
            timestamp = self.timestamp,
        )
        byte_str = base_str.encode(encoding='utf-8')
        encode_str = base64.b64encode(byte_str)
        return encode_str.decode()

    def create_sig(self):
        """
        生成七陌请求参数 sig
        """
        base_str = '{account_id}{account_api_secret}{timestamp}'.format(
            account_id = self.account_id,
            account_api_secret = self.account_api_secret,
            timestamp = self.timestamp,
        )
        md5 = hashlib.md5()
        md5.update(base_str.encode())
        sig = md5.hexdigest()
        return sig.upper()


if __name__ == '__main__':

    token = Token()
    print(token.create_authorization())
    print(token.create_sig())
